// pages/medal/index.js
const app = getApp()
import { medalList } from '../../api/medal.js'
import { medalLight } from '../../api/medal.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bindIndex:'',//当前点击的列表index
    medalList:[],
    shareList:['发送给微信好友', '生成海报'],
    showShare:true
  },
  //获取勋章列表
  _medalList(){
    medalList().then(res => {
      this.setData({medalList:res.data })
    })
  },
  onShow(){
    this._medalList()
  },
  condition(e) {
    let d = e.currentTarget.dataset
    let status = d.status
    if (status == 0) {
      wx.showModal({
        title: d.name,
        content: '报名参加完成次数达到' + d.condition + '次',
        showCancel:false,
        confirmText: '好哒~',
        confirmColor: '#576B95'
      })
    } else if (status == 1) {
      this.state1(e)
    } else{
      this.share(e)
    }
  },
  showShareOrHide() {
    this.setData({showShare: !this.data.showShare})
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this._medalList()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  },
  //可点亮
  state1(e){
    let d = e.currentTarget.dataset
    this.setData({ bindIndex:d.index})
    let medalid =d.medalid
    wx.showModal({
      title: d.name,
      content: '报名参加完成次数达到'+d.condition+'次',
      confirmText:'点亮勋章',
      confirmColor:'#576B95',
      success:res=>{
        if (res.confirm) {
          let { bindIndex, medalList } = this.data
          medalLight(medalid).then(res => {
          this.setData({['medalList[' + bindIndex +'].status']:2})
            this.share(e)
          })
        }
      }
    })
  },
  //分享
  share(e) {
    let d = e.currentTarget.dataset
    this.setData({ bindIndex: d.index })
    wx.showModal({
      title: d.name,
      content: '报名参加完成次数达到' + d.condition + '次',
      cancelText:'确定',
      confirmColor:'#576B95',
      confirmText:'分享荣誉',
      confirmColor:'#576B95',
      success:res=>{
        if (res.confirm) {
          setTimeout(() => {
            this.showShareOrHide()
          }, 1000);
        }
      }
    })
  },
  shareType(e){
    if (e.currentTarget.dataset.index == 1) {
      let { bindIndex, medalList } = this.data
      wx.navigateTo({
        url: `/pages/medal_share/index?img=${medalList[bindIndex].image}&name=${medalList[bindIndex].name}&condition=${medalList[bindIndex].condition}`,
      })
    }
  }
})